package com.hxkcloud.patient.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.interfaces.OnRefreshLayoutCanScroll;
import com.hxkcloud.patient.views.SwipeLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ray_lei on 2016/7/6.
 */
public class XueyaDetailListAdapter extends RecyclerView.Adapter<ViewHolder>{


    private Context mContext;
    private List<String> datas;
    private RecyclerView recyclerview;
    private LayoutInflater mInflater;

    private ArrayList<SwipeLayout> mOpenItems = new ArrayList<SwipeLayout>();

    private PostOptionsListener optionsListener;

    public XueyaDetailListAdapter(Context mContext, List<String> datas,
                                  RecyclerView recyclerview) {
        this.mContext = mContext;
        this.datas = datas;
        this.recyclerview = recyclerview;
        mInflater = LayoutInflater.from(mContext);
    }

    public void setData(List<String> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    public void clearData() {
        datas.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        // TODO Auto-generated method stub
        return datas.size();
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        // TODO Auto-generated method stub


       final Holder mHolder = (Holder) holder;

        //必须添加的点击事件
        mHolder.ll_main.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mOpenItems.size() > 0) {
                    closeAll();
                } else {

                }
            }
        });
        //必须添加的点击事件
        mHolder.ll_back.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (mOpenItems.size() > 0) {
                    closeAll();
                }
            }
        });
        //编辑
        mHolder.tv_item_add_digest.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mOpenItems.size() > 0) {
                    closeAll();
                }
            }
        });
        //删除
        mHolder.tv_item_delete.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mOpenItems.size() > 0) {
                    closeAll();
                }
                optionsListener.PostOptions(mHolder.tv_item_delete, position, "delete");
            }
        });

        mHolder.sl_item.setStopPulltorefreshScrollerListener(new SwipeLayout.StopPulltorefreshScrollerListener() {

            @Override
            public void setCanScroll(boolean canScoller) {
                // TODO Auto-generated method stub
                if (mCanScrollListener != null) {
                    mCanScrollListener.setCanScrollMove(canScoller);
                }
            }
        });

        //管理侧滑swipelayout，只允许同时有一个侧滑弹出
        mHolder.sl_item.setmOnDragStateChangeListener(new SwipeLayout.OnDragStateChangeListener() {


            @Override
            public void onStartOpen(SwipeLayout layout) {
                closeAll();
                mOpenItems.add(layout);
            }

            @Override
            public void onStartClose(SwipeLayout layout) {

            }

            @Override
            public void onOpen(SwipeLayout layout) {

            }

            @Override
            public void onDragging(SwipeLayout layout) {


            }

            @Override
            public void onClose(SwipeLayout layout) {
                mOpenItems.remove(layout);
            }

        });



    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


        View v = mInflater.inflate(R.layout.item_xueyadetaillist_layout, null);
        ViewHolder holer = new Holder(v);

        return holer;
    }

    @Override
    public int getItemViewType(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public void closeAll() {
        for (SwipeLayout item : mOpenItems) {
            item.close(true);
        }
    }

    private OnRefreshLayoutCanScroll mCanScrollListener;

    public void setOnRefreshLayoutCanScroll(OnRefreshLayoutCanScroll l) {
        mCanScrollListener = l;
    }

    public void setPostOptionsListener(PostOptionsListener optionsListener){
        this.optionsListener = optionsListener;
    }

    public interface PostOptionsListener{
        public void PostOptions(View view,int position,String optionType);
    }

    /*
     * Holder
     */
    public class Holder extends ViewHolder {

        SwipeLayout sl_item;//侧滑控件
        LinearLayout ll_main;
        LinearLayout ll_back;

        TextView tv_item_add_digest;//加精按钮
        TextView tv_item_delete;//删除按钮


        TextView timeTV;
        TextView value1TV;
        TextView value2TV;


        public Holder(View view) {
            super(view);
            timeTV = (TextView) view.findViewById(R.id.detail_tv_time);
            value1TV = (TextView) view.findViewById(R.id.detail_tv_value1);
            value2TV = (TextView) view.findViewById(R.id.detail_tv_value2);

            sl_item = (SwipeLayout) view.findViewById(R.id.sl_item);
            tv_item_add_digest = (TextView) view.findViewById(R.id.tv_item_add_digest);
            tv_item_delete = (TextView) view.findViewById(R.id.tv_item_delete);
            ll_main = (LinearLayout) view.findViewById(R.id.ll_main);
            ll_back = (LinearLayout) view.findViewById(R.id.ll_back);
        }
    }





}
